{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预处理\n",
    "\n",
    "1. 标签处理\n",
    "2. 标签编码\n",
    "3. 可视化编码过程\n",
    "4. 定义预处理类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "aTsg5j62xfnk"
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import torch as t\n",
    "import numpy as np\n",
    "import torchvision.transforms.functional as ff\n",
    "from torch.utils.data import Dataset\n",
    "from PIL import Image\n",
    "import torchvision.transforms as transforms\n",
    "import matplotlib.pyplot as plt # plt 用于显示图片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "tlE9Ahg4zCUb"
   },
   "source": [
    "**1 处理标签文件中colormap的数据：** dataset.py read_color_map方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 402
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 694,
     "status": "ok",
     "timestamp": 1586088525904,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "KiD7fbjc0Ie4",
    "outputId": "9f8a8808-3fbe-4e03-a062-dd41e1d678f6"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>r</th>\n",
       "      <th>g</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Sky</td>\n",
       "      <td>128</td>\n",
       "      <td>128</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Building</td>\n",
       "      <td>128</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Pole</td>\n",
       "      <td>192</td>\n",
       "      <td>192</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Road</td>\n",
       "      <td>128</td>\n",
       "      <td>64</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Sidewalk</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Tree</td>\n",
       "      <td>128</td>\n",
       "      <td>128</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SignSymbol</td>\n",
       "      <td>192</td>\n",
       "      <td>128</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Fence</td>\n",
       "      <td>64</td>\n",
       "      <td>64</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Car</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Pedestrian</td>\n",
       "      <td>64</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Bicyclist</td>\n",
       "      <td>0</td>\n",
       "      <td>128</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>unlabelled</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          name    r    g    b\n",
       "0          Sky  128  128  128\n",
       "1     Building  128    0    0\n",
       "2         Pole  192  192  128\n",
       "3         Road  128   64  128\n",
       "4     Sidewalk    0    0  192\n",
       "5         Tree  128  128    0\n",
       "6   SignSymbol  192  128  128\n",
       "7        Fence   64   64  128\n",
       "8          Car   64    0  128\n",
       "9   Pedestrian   64   64    0\n",
       "10   Bicyclist    0  128  192\n",
       "11  unlabelled    0    0    0"
      ]
     },
     "execution_count": 44,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd_label_color = pd.read_csv('./CamVid/class_dict.csv', sep=',')\n",
    "pd_label_color"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1200,
     "status": "ok",
     "timestamp": 1586088529191,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "VBhP0eY71EIC",
    "outputId": "ac4ec01f-c2de-4695-de9d-b31c8d2779d4"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=12, step=1)"
      ]
     },
     "execution_count": 45,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd_label_color.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 709,
     "status": "ok",
     "timestamp": 1586088530733,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "y_4UMp-g1l6O",
    "outputId": "0a8d6d65-5b15-44c3-ae67-b0bcd2d7e8c5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 46,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pd_label_color.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 101
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1059,
     "status": "ok",
     "timestamp": 1586088533224,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "lTV7BzGC1wNz",
    "outputId": "703f9ce6-ce64-46e8-ef50-3e18501e825e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name    Pole\n",
       "r        192\n",
       "g        192\n",
       "b        128\n",
       "Name: 2, dtype: object"
      ]
     },
     "execution_count": 47,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pandas.iloc方法提供了基于整数的索引方式，跟 python自身的list 的索引方式是十分类似\n",
    "pd_label_color.iloc[2]  # index location"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 50
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1235,
     "status": "ok",
     "timestamp": 1586088536219,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "ljYXkuY43ITV",
    "outputId": "020aee9a-a1da-4627-eb37-80c2b9aaa52e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "192\n",
      "128\n"
     ]
    }
   ],
   "source": [
    "tmp = pd_label_color.iloc[2]\n",
    "print(tmp['r'])\n",
    "print(tmp['b'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 67
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1506,
     "status": "ok",
     "timestamp": 1586009704843,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "rqmrpKWK0EwT",
    "outputId": "76924c46-a792-4610-964e-5126e32879c9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Sky' 'Building' 'Pole' 'Road' 'Sidewalk' 'Tree' 'SignSymbol' 'Fence'\n",
      " 'Car' 'Pedestrian' 'Bicyclist' 'unlabelled']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 6,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_value = pd_label_color['name'].values  # ndarray type\n",
    "print(name_value)\n",
    "num_class = len(name_value)\n",
    "num_class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 255
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1048,
     "status": "ok",
     "timestamp": 1586088538642,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "pfxSCML90_c9",
    "outputId": "e0500f6b-0bce-4cd4-8194-2403205801a4"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[128, 128, 128], [128, 0, 0], [192, 192, 128], [128, 64, 128], [0, 0, 192], [128, 128, 0], [192, 128, 128], [64, 64, 128], [64, 0, 128], [64, 64, 0], [0, 128, 192], [0, 0, 0]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[128, 128, 128],\n",
       "       [128,   0,   0],\n",
       "       [192, 192, 128],\n",
       "       [128,  64, 128],\n",
       "       [  0,   0, 192],\n",
       "       [128, 128,   0],\n",
       "       [192, 128, 128],\n",
       "       [ 64,  64, 128],\n",
       "       [ 64,   0, 128],\n",
       "       [ 64,  64,   0],\n",
       "       [  0, 128, 192],\n",
       "       [  0,   0,   0]], dtype=uint8)"
      ]
     },
     "execution_count": 49,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出csv文件中的不同物体的颜色映射标签colormap\n",
    "colormap = []\n",
    "for i in range(len(pd_label_color.index)):\n",
    "    # 通过行号索引行数据\n",
    "    tmp = pd_label_color.iloc[i]\n",
    "    color = [tmp['r'], tmp['g'], tmp['b']]\n",
    "    colormap.append(color)\n",
    "print(colormap)\n",
    "\n",
    "cm = np.array(colormap).astype('uint8')\n",
    "cm\n",
    "# 注意这里的索引对应关系，cm[0] = [128, 128, 128] 这里的索引 0 也对应着 sky 这种标签类别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "7HBuVDz6aS2w"
   },
   "source": [
    "**2 标签编码**\n",
    "\n",
    "　　这里可以对应到算法与数据结构中的`哈希算法`，哈希算法是为了形成一种多对一或一对一的映射关系，从而加快检索和查询的效率（这里只是简单提一下，不影响对课程的整体理解）\n",
    "\n",
    "　　函数`encode_label_color(colormap)`，形成颜色到标签的一一对应关系，下面使用了类似256进制的方法映射每一个color map里的像素点到它所表示的类别（用哈希函数做了一张哈希表）\n",
    "* 希函数：(cm[0] * 256 + cm[1]) * 256 + cm[2] \n",
    "* 哈希映射：cm2lbl[(cm[0] * 256 + cm[1]) * 256 + cm[2]] = i\n",
    "* 哈希表：cm2lbl  \n",
    "\n",
    "　　原理：一个像素点如:`P（128，64，128）`由编码函数`(P[0] * 256 + P[1]) * 256 + P[2]`转化为整数`（8405120）`,将这个数字作为像素点P在cm2lbl表中的索引`cm2lbl[8405120]`去查询像素点`P(128,64,128)`所对应的类别`i`。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 50
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1149,
     "status": "ok",
     "timestamp": 1586088543428,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "nnmH0537-mig",
    "outputId": "c031962f-316e-496c-994e-fcc69d43b8b8"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11.  0.  0. ...  0.  0.  0.]\n",
      "66.0\n"
     ]
    }
   ],
   "source": [
    "def encode_label_pix(colormap):\n",
    "    cm2lbl = np.zeros(256 ** 3)\n",
    "    for i, cm in enumerate(colormap):\n",
    "        cm2lbl[(cm[0] * 256 + cm[1]) * 256 + cm[2]] = i\n",
    "    return cm2lbl\n",
    "\n",
    "cm2lbl = encode_label_pix(colormap)\n",
    "print(cm2lbl)\n",
    "print(cm2lbl.sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "6hw6abrVhR5v"
   },
   "source": [
    "**2.1可视化标签编码过程**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 265
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1116,
     "status": "ok",
     "timestamp": 1586088546550,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "eo9f_BRVPpcK",
    "outputId": "81a110d9-b1e7-46b6-f000-76100709da4e"
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\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La0pwBe6dfgI\nk3sAjlnT0UzCe+yS1+a2jpj8I7MXeIPJcffHmJzHuRd4Cvh74Lhu7EbghiXvvar7W9oNfHTEenYz\nOT+w/+9o/zdKJwF3HeizndfDy2WlRngFndQIwy41wrBLjTDsUiMMu9QIwy41wrBLjfg/kMuhC0dn\noYQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "tags": []
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 从label中扣取一部分展示出来 展示标签是如何制作的\n",
    "label_path = '/content/CamVid/train_labels/0001TP_006690_L.png'\n",
    "label = Image.open(label_path).convert('RGB')\n",
    "label = np.array(label)\n",
    "demo_label = label[280:295, 280:295, :]  #shape(15,15,3)\n",
    "plt.imshow(demo_label) # 显示图片\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 806
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1334,
     "status": "ok",
     "timestamp": 1586088550205,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "Y0Gg4XIWTap8",
    "outputId": "505ca7b3-18af-49a0-8481-e954b9746a84"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64 128 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64 128 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64  64 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64 128 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64 128 128 128 128 128 128 128 128 128]\n",
      " [ 64  64  64  64  64  64 128 128 128 128 128 128 128 128 128]]\n",
      "----------------------------------------------------------------\n",
      "[[ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0 64 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0 64 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0  0 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0 64 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0 64 64 64 64 64 64 64 64 64]\n",
      " [ 0  0  0  0  0  0 64 64 64 64 64 64 64 64 64]]\n",
      "----------------------------------------------------------------\n",
      "[[128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]\n",
      " [128 128 128 128 128 128 128 128 128 128 128 128 128 128 128]]\n"
     ]
    }
   ],
   "source": [
    "# 对照颜色表，这上图是car和road的分界线\n",
    "# 上面一小块图像的数值结构\n",
    "data = np.array(demo_label)\n",
    "print(data[:, :, 0])\n",
    "print('--------'*8)\n",
    "print(data[:, :, 1])\n",
    "print('--------'*8)\n",
    "print(data[:, :, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 790
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1101,
     "status": "ok",
     "timestamp": 1586088556794,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "9SYnxRLbwgSI",
    "outputId": "4583760b-31ff-4b27-dff0-553364df3215"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]\n",
      " [4194432 4194432 4194432 4194432 4194432 4194432 8405120 8405120 8405120\n",
      "  8405120 8405120 8405120 8405120 8405120 8405120]]\n",
      "----\n",
      "[[8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3. 3.]\n",
      " [8. 8. 8. 8. 8. 8. 3. 3. 3. 3. 3. 3. 3. 3. 3.]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵化批量操作像素点的编码由（r, g, b） ---> index ---> identity\n",
    "def encode_label_img(img):\n",
    "    data = np.array(img, dtype='int32')\n",
    "    idx = (data[:, :, 0] * 256 + data[:, :, 1]) * 256 + data[:, :, 2]  # 这是一片数字\n",
    "    print(idx)\n",
    "    print('----')\n",
    "    print(cm2lbl[idx])  # 由一片索引带出来的一片表示类别的数值\n",
    "    return np.array(cm2lbl[idx], dtype='int64')  # 使用numpy 改变一下数据的类型，增加计算精度，int8-->int64\n",
    "\n",
    "decoded_demo_label = image2label(demo_label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5elVSsoT8Nqf"
   },
   "source": [
    "**定义一个类来描述标签像素编码这个行为**\n",
    "\n",
    "　　将上面介绍的函数功能都整合到一个类里，使代码结构更清晰"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "nNiMltOIzlfQ"
   },
   "outputs": [],
   "source": [
    "class LabelProcessor():\n",
    "    \"\"\"对标签图像的编码\"\"\"\n",
    "    def __init__(self, file_path):\n",
    "\n",
    "        self.colormap = self.read_color_map(file_path)\n",
    "\n",
    "        self.cm2lbl = self.encode_label_pix(self.colormap)\n",
    "\n",
    "    def read_color_map(self, file_path):\n",
    "        pd_label_color = pd.read_csv(file_path, sep=',')\n",
    "        name_value = pd_label_color['name'].values  # ndarray type\n",
    "        num_class = len(name_value)\n",
    "\n",
    "        colormap = []\n",
    "        for i in range(len(pd_label_color.index)):\n",
    "            tmp = pd_label_color.iloc[i]\n",
    "            color = [tmp['r'], tmp['g'], tmp['b']]\n",
    "            colormap.append(color)\n",
    "        cm = np.array(colormap).astype('uint8')\n",
    "        return colormap\n",
    "  \n",
    "    def encode_label_pix(self, colormap):\n",
    "        cm2lbl = np.zeros(256 ** 3)\n",
    "        for i, cm in enumerate(colormap):\n",
    "            cm2lbl[(cm[0] * 256 + cm[1]) * 256 + cm[2]] = i\n",
    "        return cm2lbl\n",
    "\n",
    "    def encode_label_img(self, img):\n",
    "    \n",
    "        data = np.array(img, dtype='int32')\n",
    "        idx = (data[:, :, 0] * 256 + data[:, :, 1]) * 256 + data[:, :, 2]  \n",
    "        return np.array(self.cm2lbl[idx], dtype='int64') \n",
    "\n",
    "class_dict_path = './CamVid/class_dict.csv'\n",
    "label_processor = LabelProcessor(class_dict_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 218
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1224,
     "status": "ok",
     "timestamp": 1586089713255,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "UCMN7-Fp4qpp",
    "outputId": "16c6078f-7b48-4acc-f761-c2d489fb8a17"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[128, 128, 128],\n",
       " [128, 0, 0],\n",
       " [192, 192, 128],\n",
       " [128, 64, 128],\n",
       " [0, 0, 192],\n",
       " [128, 128, 0],\n",
       " [192, 128, 128],\n",
       " [64, 64, 128],\n",
       " [64, 0, 128],\n",
       " [64, 64, 0],\n",
       " [0, 128, 192],\n",
       " [0, 0, 0]]"
      ]
     },
     "execution_count": 63,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_processor.colormap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1139,
     "status": "ok",
     "timestamp": 1586089715839,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "fFRFkQ1j5XQm",
    "outputId": "e7571083-8a04-4ee7-d74f-6edd9547be6b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "66.0"
      ]
     },
     "execution_count": 64,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_processor.cm2lbl.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 269
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1146,
     "status": "ok",
     "timestamp": 1586089718243,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "-1CgwLbc7EP_",
    "outputId": "708e9a8b-c052-4de1-dcec-50cf631354a6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3, 3]])"
      ]
     },
     "execution_count": 65,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_processor.encode_label_img(demo_label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 104
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1272,
     "status": "ok",
     "timestamp": 1586067414596,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "b4eeR63Y6x1Y",
    "outputId": "50c61750-cd65-4da8-8b0c-454da44a9cd0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['0001TP_006720.png', '0001TP_006810.png', '0001TP_006750.png', '0001TP_006690.png', '0001TP_006780.png']\n",
      "['./CamVid/train/0001TP_006720.png', './CamVid/train/0001TP_006810.png', './CamVid/train/0001TP_006750.png', './CamVid/train/0001TP_006690.png', './CamVid/train/0001TP_006780.png']\n",
      "['0001TP_006750_L.png', '0001TP_006720_L.png', '0001TP_006690_L.png', '0001TP_006780_L.png', '0001TP_006810_L.png']\n",
      "['./CamVid/train_labels/0001TP_006750_L.png', './CamVid/train_labels/0001TP_006720_L.png', './CamVid/train_labels/0001TP_006690_L.png', './CamVid/train_labels/0001TP_006780_L.png', './CamVid/train_labels/0001TP_006810_L.png']\n"
     ]
    }
   ],
   "source": [
    "TRAIN_ROOT = './CamVid/train'\n",
    "TRAIN_LABEL = './CamVid/train_labels'\n",
    "# VAL_ROOT = './CamVid/val'\n",
    "# VAL_LABEL = './CamVid/val_labels'\n",
    "# TEST_ROOT = './CamVid/test'\n",
    "# TEST_LABEL = './CamVid/test_labels'\n",
    "\n",
    "def read_file(path):\n",
    "    files_list = os.listdir(path)\n",
    "    print(files_list)\n",
    "    file_path_list = [os.path.join(path, img) for img in files_list]    \n",
    "    print(file_path_list)\n",
    "    file_path_list.sort()\n",
    "    return file_path_list\n",
    "\n",
    "\n",
    "train_imgs = read_file(TRAIN_ROOT)\n",
    "train_labels = read_file(TRAIN_LABEL)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "nYaLpze0v5vj"
   },
   "outputs": [],
   "source": [
    "class CamvidDataset(Dataset):\n",
    "    def __init__(self, file_path=[], crop_size=None):\n",
    "        \"\"\"para:\n",
    "            mode(string): 数据使用模式，是训练数据、验证数据还是测试数据\n",
    "            file_path(list): 数据和标签路径,列表元素第一个为图片路径，第二个为标签路径\n",
    "        \"\"\"\n",
    "        # 1 正确读入图片和标签路径\n",
    "        if len(file_path) != 2:\n",
    "            raise ValueError(\"同时需要图片和标签文件夹的路径，图片路径在前\")\n",
    "        self.img_path = file_path[0]\n",
    "        self.label_path = file_path[1] \n",
    "        # 2 从路径中取出图片和标签数据的文件名保持到两个列表当中（程序中的数据来源）\n",
    "        self.imgs = self.read_file(self.img_path)\n",
    "        self.labels = self.read_file(self.label_path)\n",
    "        # 3 初始化数据处理函数设置\n",
    "        self.crop_size = crop_size\n",
    "        \n",
    "    \n",
    "    def __getitem__(self, index):\n",
    "        img = self.imgs[index]\n",
    "        label = self.labels[index]\n",
    "        # 从文件名中读取数据（图片和标签都是png格式的图像数据）\n",
    "        img = Image.open(img)\n",
    "        label = Image.open(label).convert('RGB')\n",
    "\n",
    "        img, label = self.center_crop(img, label, self.crop_size)\n",
    "\n",
    "        img, label = self.img_transform(img, label)\n",
    "        # print('处理后的图片和标签大小：',img.shape, label.shape)\n",
    "        sample = {'img': img, 'label': label}\n",
    "\n",
    "        return sample\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.imgs)\n",
    "\n",
    "    \n",
    "    def read_file(self, path):\n",
    "        \"\"\"从文件夹中读取数据\"\"\"\n",
    "        files_list = os.listdir(path)\n",
    "        file_path_list = [os.path.join(path, img) for img in files_list]    \n",
    "        file_path_list.sort()\n",
    "        return file_path_list\n",
    "\n",
    "    def center_crop(self, data, label, crop_size):\n",
    "        \"\"\"裁剪输入的图片和标签大小\"\"\"\n",
    "        data = ff.center_crop(data, crop_size)\n",
    "        label = ff.center_crop(label, crop_size)\n",
    "        return data, label\n",
    "\n",
    "    def img_transform(self, img, label):\n",
    "        \"\"\"对图片和标签做一些数值处理\"\"\"\n",
    "        label = np.array(label)  # 以免不是np格式的数据\n",
    "        label = Image.fromarray(label.astype('uint8'))\n",
    "        transform_img = transforms.Compose(\n",
    "            [\n",
    "                transforms.ToTensor(),\n",
    "                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n",
    "            ]\n",
    "        )\n",
    "        img = transform_img(img)\n",
    "        label = label_processor.encode_label_img(label)\n",
    "        label = t.from_numpy(label)\n",
    "\n",
    "        return img, label\n",
    "\n",
    "\n",
    "TRAIN_ROOT = './CamVid/train'\n",
    "TRAIN_LABEL = './CamVid/train_labels'\n",
    "crop_size = (352, 480)\n",
    "Cam_train = CamvidDataset([TRAIN_ROOT, TRAIN_LABEL], crop_size)\n",
    "# Cam_val = CamvidDataset(\"val\", input_size, img_transform)\n",
    "# Cam_test = CamvidDataset(\"test\", input_size, img_transform)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1391,
     "status": "ok",
     "timestamp": 1586089728352,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "TSB6RP2PEdjY",
    "outputId": "41966186-3142-47cd-9b2b-0cc55a5cdb93"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "num_items = Cam_train.__len__()\n",
    "print(num_items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 521
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1115,
     "status": "ok",
     "timestamp": 1586089730229,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "vdtTy1xvFpZP",
    "outputId": "5ffad363-956c-441c-d9c8-6540b3b0e236"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'img': tensor([[[-1.1932, -1.1247, -1.0904,  ..., -1.7754, -1.7412, -1.8268],\n",
       "          [-1.3644, -1.1589, -1.0904,  ..., -1.7412, -1.7925, -1.8097],\n",
       "          [-1.4500, -1.2445, -1.1760,  ..., -1.7925, -1.8439, -1.7925],\n",
       "          ...,\n",
       "          [-1.8439, -1.8439, -1.8439,  ..., -1.4500, -1.5014, -1.5699],\n",
       "          [-1.8782, -1.8610, -1.8610,  ..., -1.6042, -1.5870, -1.5870],\n",
       "          [-1.8439, -1.8439, -1.8439,  ..., -1.7069, -1.6727, -1.5699]],\n",
       " \n",
       "         [[-1.0203, -0.8627, -0.7927,  ..., -1.6681, -1.6331, -1.7381],\n",
       "          [-1.1779, -0.8978, -0.7927,  ..., -1.6331, -1.6856, -1.7206],\n",
       "          [-1.2479, -0.9853, -0.8978,  ..., -1.6331, -1.6856, -1.6331],\n",
       "          ...,\n",
       "          [-1.7381, -1.7381, -1.7381,  ..., -1.1429, -1.1954, -1.3004],\n",
       "          [-1.7731, -1.7556, -1.7556,  ..., -1.3004, -1.2829, -1.3179],\n",
       "          [-1.7381, -1.7381, -1.7381,  ..., -1.4055, -1.3704, -1.3004]],\n",
       " \n",
       "         [[-0.7413, -0.6890, -0.6367,  ..., -1.4036, -1.3339, -1.3861],\n",
       "          [-0.9156, -0.7238, -0.6367,  ..., -1.3687, -1.3861, -1.3861],\n",
       "          [-0.9853, -0.8110, -0.7413,  ..., -1.3513, -1.4036, -1.3513],\n",
       "          ...,\n",
       "          [-1.4733, -1.4733, -1.4733,  ..., -0.8633, -0.8981, -0.9504],\n",
       "          [-1.5081, -1.4907, -1.4907,  ..., -0.9853, -0.9678, -0.9678],\n",
       "          [-1.4733, -1.4733, -1.4733,  ..., -1.1247, -1.0724, -0.9853]]]),\n",
       " 'label': tensor([[ 1,  1,  1,  ...,  1,  1,  1],\n",
       "         [ 1,  1,  1,  ...,  1,  1,  1],\n",
       "         [ 1,  1,  1,  ...,  1,  1,  1],\n",
       "         ...,\n",
       "         [ 4,  4,  4,  ..., 11, 11, 11],\n",
       "         [ 4,  4,  4,  ..., 11, 11, 11],\n",
       "         [ 4,  4,  4,  ..., 11, 11, 11]])}"
      ]
     },
     "execution_count": 68,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Cam_train.__getitem__(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 168
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1502,
     "status": "ok",
     "timestamp": 1586088595041,
     "user": {
      "displayName": "Rongfan Liao",
      "photoUrl": "",
      "userId": "07803922812103577726"
     },
     "user_tz": -480
    },
    "id": "3nXOYRdBG1YR",
    "outputId": "e6993297-6746-4ca7-c69e-552ffd11f42e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "batch[0]:\n",
      "torch.Size([2, 3, 352, 480])\n",
      "torch.Size([2, 352, 480])\n",
      "batch[1]:\n",
      "torch.Size([2, 3, 352, 480])\n",
      "torch.Size([2, 352, 480])\n",
      "batch[2]:\n",
      "torch.Size([1, 3, 352, 480])\n",
      "torch.Size([1, 352, 480])\n"
     ]
    }
   ],
   "source": [
    "# 在训练的时候使用torch的DataLoader函数加载数据\n",
    "from torch.utils.data import DataLoader\n",
    "train_data = DataLoader(Cam_train, batch_size=2, shuffle=True, num_workers=1)\n",
    "for i, sample in enumerate(train_data):\n",
    "    print('batch[%d]:' %i)\n",
    "    print(sample['img'].shape)\n",
    "    print(sample['label'].shape)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyMaJPtlMHur6DonwEyZLw5h",
   "collapsed_sections": [],
   "name": "data process and load.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
